/**
 * requires preferences.fx3.js
 */

function searchxssObj (myWindow)
{
	this.myPreferencesController = new PreferencesController();
	this.construct(myWindow);
}

searchxssObj.prototype = {
	field             : null,
	postDataField     : null,
	referrerDataField : null,
	currentFocusField : null,
	toolbar           : null,
	myWindow          : null,
	BrowserWindow     : null,
	TabBrowser        : null,
	tabManager        : null,
	anonFocusFunct    : null,

	searchxssUsefullStrings:	{
		pi: "3,14159265",
		phi: "1.618033988749895",
		piBig: "3,14159265358979323846264338327950288419716939937510",
		lorem: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
		fibonacci: "0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, ...",
		alert: "alert(String.fromCharCode(88, 83, 83))"
	},

	construct:  function(myWindow){
		this.myWindow = myWindow;

		const kWindowMediatorContractID = "@mozilla.org/appshell/window-mediator;1";
		const kWindowMediatorIID        = Components.interfaces.nsIWindowMediator;
		const kWindowMediator           = Components.classes[kWindowMediatorContractID].getService(kWindowMediatorIID);

		this.BrowserWindow = kWindowMediator.getMostRecentWindow("navigator:browser");
		this.TabBrowser    = this.BrowserWindow.getBrowser();

		this.myWindow.addEventListener( "load",   this, false );
		this.myWindow.addEventListener( "unload", this, false );
		
		this.orgurl=null;
	},
	
	useCurrentUrl:	function(){
		var sessionHistory = this.TabBrowser.selectedBrowser.webNavigation.sessionHistory;
		
		if( sessionHistory.index == -1 ) 
			return;

		var entry = sessionHistory.getEntryAtIndex(sessionHistory.index, 0);
		entry = entry.QueryInterface(Components.interfaces.nsISHEntry);

		var postString = '';
		if (entry.postData){
			try {
				var postDataStream = entry.postData;
				postDataStream.QueryInterface(Components.interfaces.nsISeekableStream).seek(0, 0);

				//create an input stream for reading the post data from
				var inputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
				inputStream.init( entry.postData );
				var postData = inputStream.read( 0xFFFFFFFF );
				var postArray = postData.split("\r\n\r\n");
				postArray.shift();
				postString = postArray.join( '\n' );
			}
			catch(e) {
				alert(e);
			}
		}

		var referringUri = entry.referrerURI;
		if (referringUri){
			referringUri = referringUri.QueryInterface(Components.interfaces.nsIURI);
			referringUri = referringUri.spec;
		}
		else{
			referringUri = "";
		}

		this.orgurl = unescape(entry.URI.spec);
	},
	
	loadUrl:	function(){
		this.useCurrentUrl();
		var uri = this.orgurl;
		uri = uri.replace( new RegExp(/\n|\r/g), '' );
		if (uri.indexOf("http://") == -1 && uri.indexOf("https://") == -1) uri = 'http://' + uri;

		var splituri=uri.split("?");
		if(splituri.length>1){
			var uripara=splituri[1].split("&");
			var values = this.myPreferencesController.preferenceStringContainer.getStrings();
			for(var j=0;j<uripara.length;j++){
				for(var i=0;i<values.length;i++){
					var temp= uri.replace(uripara[j],uripara[j].split("=")[0]+"="+values[i]);
					this.TabBrowser.addTab(temp);
				}
			}
		}
		return checkxss();
	},

	click:	function(e){
	   if (e.button == 0) {
		   this.loadUrl();
	   }
	},

	//TODO
	checkxss:	function(){
	},

	gotoSearchXssHome:	function(){
		this.TabBrowser.selectedTab=this.TabBrowser.addTab("http://code.google.com/p/searchxss/");
	},

	//open setting dialog
	openDialogSetting:	function() {
		window.openDialog('chrome://searchxss/content/searchxssSetting.xul', null,'chrome,centerscreen,dependent',this.myPreferencesController);
	}
}
